<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@page import="java.io.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
		<title>Insert title here</title>
	</head>
	<body>
		<%
			if (request.getAttribute("nombreArchivo") != null) {

				
/*	  /////////// Primer solucion			
					response.setContentType("application/octet-stream");
					
					//initialize the http content-disposition header to indicate a file attachment with the filename request.getAttribute("nombreArchivo")
					String disHeader = "attachment; filename=\"" + (String) request.getAttribute("nombreArchivo") + "\"";
					response.setHeader("Content-Disposition", disHeader);
					
					// transfer the file byte-by-byte to the response object
					ByteArrayInputStream bais = new ByteArrayInputStream((byte[])request.getAttribute("contenido"));
					int i;
					while ((i=bais.read())!=-1)
					{
					   out.write(i);
					}
					bais.close();
*/

// Solucion intermedia.

/*
System.out.println("Empezando todo");
     ServletOutputStream myOut=null;
	 myOut = response.getOutputStream( );
     
     
     //set response headers
     response.setContentType("application/octet-stream");
   //  response.setContentType("text/html");

System.out.println("Sigue 1");
     response.addHeader( "Content-Disposition","attachment; filename=\""+request.getAttribute("nombreArchivo") + "\"" );
     byte[] contenido = (byte[]) request.getAttribute("contenido");
     int size = contenido.length;
     
     
     //response.setContentLength( size);
     
System.out.println("Sigue 2");

// Metodo todo de una.
  myOut.write(contenido);
  //out.write(contenido);
System.out.println("Sigue 3");
 */    

/*
// Metodo byte por byte.
ByteArrayInputStream bais = new ByteArrayInputStream(contenido);
int i;
System.out.println("Procesando salida:");
System.out.println("Hay " + contenido.length + " bytes para procesar.");
while ((i=bais.read())!=-1)
{
System.out.print(i);
	myOut.write(i);
}
bais.close();
*/
     //read from the file; write to the ServletOutputStream
    
    // while((readBytes = buf.read( )) != -1)
    //   myOut.write(readBytes);
    

/*

//////////////////////////   Segunda solucion
File f = (File)request.getAttribute("archivoTemporal");
		        int length = 0;
		        ServletOutputStream op = response.getOutputStream();
		        ServletContext context = getServletConfig().getServletContext();
		        String mimetype = context.getMimeType( ((File)request.getAttribute("archivoTemporal")).getAbsolutePath() );

		        //
		        //  Set the response and go!
		        //
		        //
		        response.setContentType( (mimetype != null) ? mimetype : "application/octet-stream" );
		        response.setContentLength( (int)f.length() );
		        response.setHeader( "Content-Disposition", "attachment; filename=\"" + request.getAttribute("nombreArchivo") + "\"" );

		        //
		        //  Stream to the requester.
		        //
		        byte[] bbuf = new byte[100000000];
		        DataInputStream in = new DataInputStream(new FileInputStream(f));


		        
		        while ((in != null) && ((length = in.read(bbuf)) != -1))
		        {
		            op.write(bbuf,0,length);
		        }

		        in.close();
*/

			}
		%>
	</body>
</html>